package com.tianma315.admin.product.rest;


import java.util.Arrays;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.product.domain.ComplaintDO;
import com.tianma315.admin.product.service.ComplaintService;
import com.tianma315.admin.product.vo.ComplaintVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

/**
 * 
 * <pre>
 * 投诉信息
 * </pre>
 * <small> 2019-07-15 17:14:02 | wen</small>
 */
@Controller
@RequestMapping("/product/complaint")
public class ComplaintController extends BaseController{
	@Autowired
	private ComplaintService complaintService;
	
	@GetMapping()
	@RequiresPermissions("product:complaint:complaint")
	String Complaint(){
	    return "product/complaint/complaint";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("product:complaint:complaint")
	public Result<Page<ComplaintVO>> list(Integer pageNumber, Integer pageSize, ComplaintVO complaintDTO){
		// 查询列表数据
        Page<ComplaintVO> page = complaintService.getComplaintVOPage(pageNumber,pageSize,complaintDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("product:complaint:add")
	String add(){
	    return "product/complaint/add";
	}

	@GetMapping("/edit/{complaintId}")
	@RequiresPermissions("product:complaint:edit")
	String edit(@PathVariable("complaintId") Integer complaintId,Model model){
		ComplaintDO complaint = complaintService.selectById(complaintId);
		model.addAttribute("complaint", complaint);
	    return "product/complaint/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("product:complaint:add")
	public Result<String> save( ComplaintDO complaint){
		complaint.setCreateBy(getUserId());
		if(complaintService.addComplaint(complaint)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("product:complaint:edit")
	public Result<String>  update( ComplaintDO complaint){
		complaintService.updateById(complaint);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("product:complaint:remove")
	public Result<String>  remove( Integer complaintId){
		if(complaintService.deleteComplaint(complaintId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("product:complaint:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] complaintIds){
//		complaintService.deleteBatchIds(Arrays.asList(complaintIds));
		for (Integer complaintId:complaintIds) {
			complaintService.deleteComplaint(complaintId);
		}
		return Result.ok();
	}
	
}
